﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace PhoneApp1
{
    public class ZdItem : Item
    {
        public ZdItem(GameBase game, int sx, int sy, int tox, int toy)
            : base(game)
        {
            lock (this.Game)
            {
                this.Type = ItemType.zd;
                this.width = 2;
                this.height = 2;
                this.SX = sx;
                this.SY = sy;
                this.x = sx;
                this.y = sy;
                this.z = 20;
                this.ToX = tox;
                this.ToY = toy;
                JsJd();
                this.sd = 100;
                this.Draw();
                timer = new Timer(Run, null, 200, 200);
                this.ID = Item.MaxID;// this.Game.Zds.Count + 1;
                this.Game.AddItem(this);
            }
        }

        private void Run(object state)
        {
            LifeTime += 200;
            double p = sd * LifeTime / 1000;
            this.x = this.SX - (int)(p * Math.Cos(this.Jd));
            this.y = this.SY - (int)(p * Math.Sin(this.Jd));
            if (this.Game.CheckIsOver(this))
            {
                this.Dispose();
            }
            else
                Draw();
        }
        public double Jd { get; set; }
        public int SX { get; set; }
        public int SY { get; set; }
        public int ToX { get; set; }
        public int ToY { get; set; }
        public double LifeTime { get; set; }

        public override void Draw()
        {
            if (this.ThisShape == null)
            {
                this.Game.CreateShape(this);

            }
            this.Game.ItemRun(this);
        }
        private void JsJd()
        {
            int xx = ToX - SX;
            int yy = ToY - SY;
            if (xx == 0)
            {
                if (yy >= 0)
                    Jd = 90;
                else
                    Jd = 270;
            }
            else
            {
                Jd = (Math.Atan(yy / xx)) * (180 / Math.PI);
            }
        }
    }
}
